﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Text;
using System.Web.Services;

using CE.Database;
using CE.Mail;
using CE.Global;
using CE.CarInsurance;
using CE.User;
using CE.Security;

public partial class car_insurance_call_me : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[General.UserId] == null || Session[General.CarId] == null)
        {
            Response.Redirect("car-quote.aspx");
        }
        if (!IsPostBack)
        {
            divCallMe.Visible = true;
            divCallMeMessage.Visible = false;
            if (Request.QueryString["sent"] != null && Request.QueryString["sent"].ToString() != string.Empty && Request.QueryString["sent"].ToString() == "1")
            {
                divCallMeMessage.Visible = true;
                divCallMe.Visible = false;
            }
            LoadTitles();
            txtUserName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtUserName.ClientID + "','" + rfvUserName.ClientID + "','" + revUserName.ClientID + "');DefaultName(this, event);");
            txtUserName.Attributes.Add("onfocus", "ClearText(this, event);");
            txtUserEmail.Attributes.Add("onblur", "RemoveSpace(this);ShowValidationEffectTwo('" + txtUserEmail.ClientID + "','" + rfvUserEmail.ClientID + "','" + revUserEmail.ClientID + "');DefaultEmail(this, event);");
            txtUserEmail.Attributes.Add("onfocus", "ClearText(this, event);");
            txtUserMobile.Attributes.Add("onblur", "RemoveSpace(this);ShowValidationEffectTwo('" + txtUserMobile.ClientID + "','" + rfvUserMobile.ClientID + "','" + revUserMobile.ClientID + "');DefaultPhone(this, event);");
            txtUserMobile.Attributes.Add("onfocus", "ClearText(this, event);");

            HttpCookie _cookie = Request.Cookies.Get("GGMobileQuote");
            if (_cookie != null)
            {
                txtUserEmail.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerEmail"), true);
                txtUserName.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerName"), true);
                txtUserMobile.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerMobile"), true);
                string _isaccept = "FALSE";
                string[] _CK = _cookie.Value.Split('&');
                if (_CK.Length > 3)
                {
                    _isaccept = MySecurity.Decrypt(_cookie.Values.Get("IsAccept"), true);
                    if (_isaccept != null && _isaccept == "TRUE")
                    {
                        hdnIsAceept.Value = "TRUE";
                    }
                }
            }
            divAgreement.Style.Add("display", "");
            if (hdnIsAceept.Value == "TRUE")
            {
                txtIsAccept.Text = "1";
                chkAggrement.Checked = true;
                divAgreement.Style.Add("display", "none");
            }
            if (Request.QueryString["qid"] != null && Request.QueryString["qid"].ToString() != string.Empty)
            {
                hdnQuoteId.Value = Request.QueryString["qid"].ToString();
            }
            if (Request.QueryString["cid"] != null && Request.QueryString["cid"].ToString() != string.Empty)
            {
                hdnCarId.Value = Request.QueryString["cid"].ToString();
            }
            if (Request.QueryString["pid"] != null && Request.QueryString["pid"].ToString() != string.Empty)
            {
                hdnPolicyId.Value = Request.QueryString["pid"].ToString();
            }
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("results.aspx?type=email&qid=" + hdnQuoteId.Value + "&cid=" + hdnCarId.Value + "&pid=" + hdnPolicyId.Value);
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            ChangeValidationEffectServerSide("callme");
            Page.Validate("callme");
            if (Page.IsValid)
            {
                Int64 _userId = 0;
                Int64 _carId = 0;
                Int64 _carResultId = 0;
                Int64 _ProviderId = 0;
                Int64 _ExistingUserId = 0;
                string ExistingEmail = "";
                if(Session[General.UserEmail] != null && Session[General.UserEmail].ToString() != string.Empty)
                {
                    ExistingEmail = Session[General.UserEmail].ToString();
                }
                if (Session[CE.Global.General.UserId] != null)
                {
                    if (!Int64.TryParse(Session[General.UserId].ToString(), out _ExistingUserId))
                    {
                        _ExistingUserId = 0;
                    }
                }
                if (Session[General.CarId] != null)
                {
                    if (!Int64.TryParse(Session[General.CarId].ToString(), out _carId))
                    {
                        _carId = 0;
                    }
                }

                #region Customer Details
                Account _useraccount = new Account();
                AccountDB _useraccountdb = new AccountDB();

                _useraccount = _useraccountdb.GetUserAccount(txtUserEmail.Text.Trim().ToLower(), 400);
                if (_useraccount != null && _useraccount.CEID > 0)
                {
                    _userId = _useraccount.CEID;
                    Profile _userprofile = new Profile();
                    ProfileDB _userprofiledb = new ProfileDB();

                    Contact _usercontact = new Contact();
                    ContactDB _usercontactdb = new ContactDB();

                    Session[CE.Global.General.StartTime] = General.GetUTCDateTime().ToString();
                    _userprofile = _userprofiledb.GetProfile(_useraccount.CEID);

                    if (_userprofile != null && _userprofile.ProfileId > 0)
                    {
                        _userprofile.SurName = string.Empty;
                        _userprofile.FirstName = Server.HtmlEncode(txtUserName.Text);
                        _userprofile.IsAcceptTC = true;
                        _userprofiledb.Insert(_userprofile);
                    }
                    _usercontact = _usercontactdb.GetContact(_useraccount.CEID);
                    if (_usercontact != null && _usercontact.ContactId > 0)
                    {
                        _usercontact.Mobile = txtUserMobile.Text.Trim();
                        _usercontactdb.Insert(_usercontact);
                    }
                    Session[CE.Global.General.UserId] = _useraccount.CEID;
                    Session[CE.Global.General.UserTypeId] = _useraccount.UserTypeId;
                    Session[CE.Global.General.UserEmail] = _useraccount.Email;
                    Session[CE.Global.General.UserName] = _userprofile.FirstName;
                    Session[CE.Global.General.IsAcceptTC] = "0";
                    if (_userprofile.IsAcceptTC == true)
                    {
                        Session[CE.Global.General.IsAcceptTC] = "1";
                    }
                    Session[CE.Global.General.UserPhoneNo] = _usercontact.Mobile;
                    _useraccount = null;
                    _useraccountdb = null;
                    _userprofile = null;
                    _userprofiledb = null;
                    _usercontact = null;
                    _usercontactdb = null;
                    if (_ExistingUserId != _userId)
                    {
                        UpdateUserIdCarDetails(_carId, _userId, _ExistingUserId, ExistingEmail, "SWITCH", txtUserEmail.Text.Trim().ToLower(), Session[General.UserName].ToString(), txtUserMobile.Text.Trim());
                    }
                }
                else
                {
                    //General.ClearAllSession(this.Page, "USER");
                    UpdateProfile(_userId, _carId);
                    if (Session[CE.Global.General.UserId] != null)
                    {
                        if (!Int64.TryParse(Session[CE.Global.General.UserId].ToString(), out _userId))
                        {
                            _userId = 0;
                        }
                    }
                    UpdateUserIdCarDetails(_carId, _userId, _ExistingUserId, ExistingEmail, "SWITCH", txtUserEmail.Text.Trim().ToLower(), Session[General.UserName].ToString(), txtUserMobile.Text.Trim());
                    SendMailToUser(txtUserEmail.Text.Trim().ToLower(), Session[General.UserName].ToString(), txtUserMobile.Text.Trim());
                }
                if (chkAggrement.Checked)
                {
                    hdnIsAceept.Value = "TRUE";
                }
                SetCookies();
                HttpCookie _cookie = Request.Cookies.Get("GGMobileQuote");
                #endregion

                if (Session[General.UserId] != null)
                {
                    if (!Int64.TryParse(Session[General.UserId].ToString(), out _userId))
                    {
                        _userId = 0;
                    }
                }
                if (Session[General.CarId] != null)
                {
                    if (!Int64.TryParse(Session[General.CarId].ToString(), out _carId))
                    {
                        _carId = 0;
                    }
                }

                if (Request.QueryString["prid"] != null && Request.QueryString["prid"].ToString() != string.Empty)
                {
                    if (!Int64.TryParse(Request.QueryString["prid"].ToString(), out _ProviderId))
                    {
                        _ProviderId = 0;
                    }
                }

                if (Request.QueryString["rid"] != null && Request.QueryString["rid"].ToString() != string.Empty)
                {
                    if (!Int64.TryParse(Request.QueryString["rid"].ToString(), out _carResultId))
                    {
                        _carResultId = 0;
                    }
                }

                if (_userId > 0 && _carId > 0 && _ProviderId > 0)
                {
                    string _strCallMeTime = ddlCallMeTime.Text;
                    string strEmail = Server.HtmlEncode(txtUserEmail.Text.Trim());
                    string strName = Server.HtmlEncode(txtUserName.Text.Trim());
                    string strMobile = Server.HtmlEncode(txtUserMobile.Text.Trim());

                    #region Call Me
                    string _message = "";
                    string _title = "";

                    _message = "Customer Name: " + Server.HtmlEncode(txtUserName.Text);
                    _title = "Call Me Now - Call Me request by " + Server.HtmlEncode(txtUserName.Text);

                    Int64 _result = 0;
                    ContactUser _cu = new ContactUser();
                    ContactUserDB _cudb = new ContactUserDB();
                    string strInsType = "";
                    if (Request.Params["instype"] != null && Request.Params["instype"].ToString() != string.Empty)
                    {
                        strInsType = Request.Params["instype"].ToString();
                    }
                    if (strInsType == "10")
                    {
                        strInsType = "1";
                    }
                    else if (strInsType == "11")
                    {
                        strInsType = "1+";
                    }
                    else if (strInsType == "21")
                    {
                        strInsType = "2+";
                    }
                    else if (strInsType == "30")
                    {
                        strInsType = "3+";
                    }
                    else if (strInsType == "31")
                    {
                        strInsType = "3+";
                    }

                    _cu.InsuranceType = "CI-" + strInsType;
                    _cu.ProductId = _carId;
                    _cu.RefCEUserId = _userId;
                    _cu.ProviderCompanyId = _ProviderId;
                    _cu.ContactNo = Server.HtmlEncode(txtUserMobile.Text);
                    _cu.FullName = Server.HtmlEncode(txtUserName.Text);
                    _cu.CallTime = Convert.ToInt16(ddlCallMeTime.Text);

                    if (Request.Params["spc"] != null)
                    {
                        if (Request.Params["spc"].ToString() == "1")
                        {
                            _cu.Info1 = "GG Promotion";
                        }
                    }

                    _result = _cudb.Insert(_cu);

                    if (_result > 0)
                    {
                        #region Hide Call me email sending - Sakthi on 11 Dec 2014
                        //_message += "<img src='http://www.gluaygluay.com/images/callme/callme-home.png' /><br/>";
                        //_message += "<br/><br/>Insurance Type : " + strInsType;
                        //_message += "<br/><br/>Full Name : " + Server.HtmlEncode(txtUserName.Text);
                        //_message += "<br/><br/>Email : " + txtUserEmail.Text;
                        //_message += "<br/><br/>Contact No : " + Server.HtmlEncode(txtUserMobile.Text);
                        //_message += "<br/><br/>Call Time : " + ddlCallMeTime.SelectedItem.Text;
                        //string _emailList = ConfigurationManager.AppSettings["Mobile.TH.CICallMeReceiver"].ToString();
                        //MyMailManager.SendNotification(_emailList, _title, _message);
                        #endregion
                    }

                    #endregion
                    Response.Redirect("call-me.aspx?rid=" + _carResultId.ToString() + "&prid=" + _ProviderId.ToString() + "&qid=" + hdnQuoteId.Value + "&cid=" + hdnCarId.Value + "&pid=" + hdnPolicyId.Value + "&sent=1");
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    private void LoadTitles()
    {
        lblCallUs.Text = Resources.CarMenu.Callus + " ";
        lblApplicationTitle.Text = Resources.CarMenu.OR + " " + Resources.CarMenu.CallYouBack;
        //lblCallMeTimeTitle.Text = Resources.CarMenu.CallMeAt;
        lblThanksTitle.Text = Resources.CarMenu.ThankYou;
        lblMessage1.Text = Resources.CarMenu.CallMeMessage;
        lblMessage2.Text = Resources.CarMenu.HotLineMessage;
        btnSubmit.Text = Resources.ProposalForm.Submit;
        //ibtnBack.ImageUrl = Resources.CarMenu.ButtonBackUrl;
        lblAggrement.Text = Resources.UserMessage.Aggrement;
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        txtUserName.Text = HttpUtility.HtmlEncode(txtUserName.Text.Trim());
        txtUserEmail.Text = HttpUtility.HtmlEncode(txtUserEmail.Text);
        txtUserMobile.Text = HttpUtility.HtmlEncode(txtUserMobile.Text);

        #region DOB
        //Day
        if (rfvCallMeTime.Enabled && rfvCallMeTime.ValidationGroup == strValidationGroup)
        {
            rfvCallMeTime.Validate();
            if (rfvCallMeTime.IsValid == false)
            {
                ddlCallMeTime.ControlStyle.CssClass = "dropdown-fields validationError";
            }
            else
            {
                ddlCallMeTime.ControlStyle.CssClass = "dropdown-fields";
            }
        }
        else
        {
            ddlCallMeTime.ControlStyle.CssClass = "dropdown-fields";
        }
        #endregion

        #region user
        //Name
        if (rfvUserName.Enabled && rfvUserName.ValidationGroup == strValidationGroup)
        {
            rfvUserName.Validate();
            if (rfvUserName.IsValid == false)
            {
                txtUserName.ControlStyle.CssClass = "text-fields validationError";
            }
            else
            {
                txtUserName.ControlStyle.CssClass = "text-fields";
            }
        }
        else
        {
            txtUserName.ControlStyle.CssClass = "text-fields";
        }

        if (revUserName.Enabled && revUserName.ValidationGroup == strValidationGroup)
        {
            if (rfvUserName.IsValid)
            {
                revUserName.Validate();
                if (revUserName.IsValid == false)
                {
                    txtUserName.ControlStyle.CssClass = "text-fields validationError";
                }
                else
                {
                    txtUserName.ControlStyle.CssClass = "text-fields";
                }
            }
        }
        else
        {
            txtUserName.ControlStyle.CssClass = "text-fields";
        }

        //Email
        if (rfvUserEmail.Enabled && rfvUserEmail.ValidationGroup == strValidationGroup)
        {
            rfvUserEmail.Validate();
            if (rfvUserEmail.IsValid == false)
            {
                txtUserEmail.ControlStyle.CssClass = "text-fields validationError";
            }
            else
            {
                txtUserEmail.ControlStyle.CssClass = "text-fields";
            }
        }
        else
        {
            txtUserEmail.ControlStyle.CssClass = "text-fields";
        }

        if (revUserEmail.Enabled && revUserEmail.ValidationGroup == strValidationGroup)
        {
            if (rfvUserEmail.IsValid)
            {
                revUserEmail.Validate();
                if (revUserEmail.IsValid == false)
                {
                    txtUserEmail.ControlStyle.CssClass = "text-fields validationError";
                }
                else
                {
                    txtUserEmail.ControlStyle.CssClass = "text-fields";
                }
            }
        }
        else
        {
            txtUserEmail.ControlStyle.CssClass = "text-fields";
        }

        //Mobile No
        if (rfvUserMobile.Enabled && rfvUserMobile.ValidationGroup == strValidationGroup)
        {
            rfvUserMobile.Validate();
            if (rfvUserMobile.IsValid == false)
            {
                txtUserMobile.ControlStyle.CssClass = "text-fields validationError";
            }
            else
            {
                txtUserMobile.ControlStyle.CssClass = "text-fields";
            }
        }
        else
        {
            txtUserMobile.ControlStyle.CssClass = "text-fields";
        }

        if (revUserMobile.Enabled && revUserMobile.ValidationGroup == strValidationGroup)
        {
            if (rfvUserMobile.IsValid)
            {
                revUserMobile.Validate();
                if (revUserMobile.IsValid == false)
                {
                    txtUserMobile.ControlStyle.CssClass = "text-fields validationError";
                }
                else
                {
                    txtUserMobile.ControlStyle.CssClass = "text-fields";
                }
            }
        }
        else
        {
            txtUserMobile.ControlStyle.CssClass = "text-fields";
        }
        #endregion
    }

    private void GetUserDetails(string CustomerEmail)
    {
        try
        {
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.GetUserAccount(CustomerEmail.ToLower(), 400);
            if (_account != null)
            {
                Session[CE.Global.General.UserId] = _account.CEID;
                Session[CE.Global.General.UserTypeId] = _account.UserTypeId;
                Session[CE.Global.General.UserEmail] = _account.Email;

                Profile _profile = new Profile();
                ProfileDB _profiledb = new ProfileDB();
                Contact _contact = new Contact();
                ContactDB _contactdb = new ContactDB();

                _contact = _contactdb.GetContact(_account.CEID);
                _profile = _profiledb.GetProfile(_account.CEID);

                if (_contact != null)
                {
                    Session[CE.Global.General.UserPhoneNo] = _contact.Mobile;
                }
                else
                {
                    Session[CE.Global.General.UserPhoneNo] = null;
                }
                if (_profile != null)
                {
                    if (_profile.FirstName != null && _profile.FirstName != string.Empty)
                    {
                        Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.FirstName) + " " + Server.HtmlDecode(_profile.SurName);
                    }
                    else
                    {
                        Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.SurName);
                    }
                    if (_profile.IsAcceptTC != null)
                    {
                        Session[CE.Global.General.IsAcceptTC] = "0";
                        if (_profile.IsAcceptTC == true)
                        {
                            Session[CE.Global.General.IsAcceptTC] = "1";
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    private string EmailAddress
    {
        get { return txtUserEmail.Text.Trim(); }
    }

    private string MobileNumber
    {
        get { return txtUserMobile.Text.Trim(); }
    }

    private string Name
    {
        get { return txtUserName.Text.Trim(); }
    }

    private void SetCookies()
    {
        try
        {
            string _isAccept = "FALSE";
            if (hdnIsAceept.Value == "TRUE")
            {
                _isAccept = "TRUE";
            }
            HttpCookie _cookie = new HttpCookie("GGMobileQuote");
            _cookie.Values.Add("CustomerName", MySecurity.Encrypt(txtUserName.Text, true));
            _cookie.Values.Add("CustomerEmail", MySecurity.Encrypt(txtUserEmail.Text, true));
            _cookie.Values.Add("CustomerMobile", MySecurity.Encrypt(txtUserMobile.Text, true));
            _cookie.Values.Add("IsAccept", MySecurity.Encrypt(_isAccept, true));
            _cookie.Expires = DateTime.Now.AddDays(90);
            Response.Cookies.Add(_cookie);
        }
        catch (Exception Ex)
        {
            Ex = null;
        }
    }

    private void GetCookies()
    {
        try
        {
            HttpCookie _cookie = Request.Cookies.Get("GGMobileQuote");
            if (_cookie != null)
            {
                txtUserName.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerName"), true);
                txtUserEmail.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerEmail"), true);
                string _mobile = MySecurity.Decrypt(_cookie.Values.Get("CustomerMobile"), true);
                txtUserMobile.Text = _mobile;
                string _isaccept = "FALSE";
                string[] _CK = _cookie.Value.Split('&');
                if (_CK.Length > 3)
                {
                    _isaccept = MySecurity.Decrypt(_cookie.Values.Get("IsAccept"), true);
                    if (_isaccept != null && _isaccept == "TRUE")
                    {
                        hdnIsAceept.Value = "TRUE";
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            Ex = null;
        }
    }

    #region Change Email - create new account

    private Int64 UpdateProfile(Int64 UserId, Int64 CarId)
    {
        Int64 _retValue = 0;

        if (UserId != 0)
        {
            Profile _profile = new Profile();
            ProfileDB _profiledb = new ProfileDB();

            Contact _contact = new Contact();
            ContactDB _contactdb = new ContactDB();

            _profile = _profiledb.GetProfile(UserId);
            _contact = _contactdb.GetContact(UserId);
            _retValue = UserId;
            if (_profile != null)
            {
                #region Update Profile

                _profile.Salutation = "";
                _profile.FirstName = HttpUtility.HtmlEncode(txtUserName.Text);
                _profile.SurName = "";
                if (_profile.DOB.Year == 1900)
                {
                    DateTime dteDOB = new DateTime(1995, 1, 1);
                    _profile.DOB = dteDOB;
                }
                if (_profile.Gender.ToString().Trim() == "")
                {
                    _profile.Gender = 'M';
                }
                _profile.Status = "Complete";
                _profile.IsAcceptTC = true;// chkAggrement.Checked;

                _retValue = _profiledb.Insert(_profile);

                if (_retValue > 0 && _contact != null)
                {
                    _contact.Mobile = txtUserMobile.Text.Trim();
                    _contact.Status = true;

                    _retValue = _contactdb.Insert(_contact);
                }

                if (_retValue > 0)
                {
                    if (_profile.FirstName != string.Empty)
                    {
                        Session[CE.Global.General.UserName] = _profile.FirstName + " " + _profile.SurName;
                    }
                    else
                    {
                        Session[CE.Global.General.UserName] = _profile.SurName;
                    }
                    Session[CE.Global.General.IsAcceptTC] = "0";
                    if (_profile.IsAcceptTC == true)
                    {
                        Session[CE.Global.General.IsAcceptTC] = "1";
                    }
                    if (_contact != null)
                    {
                        Session[General.UserPhoneNo] = _contact.Mobile;
                    }
                    Session[CE.Global.General.UserProfileAndContactComplete] = "1";
                }

                _profile = null;
                _contact = null;
                #endregion
            }
        }
        else
        {
            _retValue = SaveNewCustomer();
            UserId = _retValue;
        }
        return _retValue;
    }

    private Int64 SaveNewCustomer()
    {
        #region New Customer
        Int64 _retVal = 0;
        if (Page.IsValid)
        {

            Account _useraccount = new Account();
            AccountDB _useraccountdb = new AccountDB();

            Profile _userprofile = new Profile();
            ProfileDB _userprofiledb = new ProfileDB();

            Contact _contact = new Contact();
            ContactDB _contactdb = new ContactDB();

            _useraccount = _useraccountdb.GetUserAccount(txtUserEmail.Text.Trim(), 400);
            if (_useraccount != null)
            {
                if (_useraccount.CEID > 0)
                {
                    _retVal = _useraccount.CEID;
                    _contact = _contactdb.GetContact(_useraccount.CEID);
                    Session[CE.Global.General.UserId] = _useraccount.CEID;
                    Session[CE.Global.General.UserTypeId] = _useraccount.UserTypeId;
                    Session[CE.Global.General.UserEmail] = _useraccount.Email;
                    Session[CE.Global.General.UserPhoneNo] = _contact.Mobile;
                    Session[CE.Global.General.StartTime] = General.GetUTCDateTime().ToString();
                    _userprofile = _userprofiledb.GetProfile(_useraccount.CEID);
                    if (_userprofile != null && _userprofile.ProfileId > 0)
                    {
                        if (_userprofile.FirstName != string.Empty)
                        {
                            Session[CE.Global.General.UserName] = _userprofile.FirstName + " " + _userprofile.SurName;
                        }
                        else
                        {
                            Session[CE.Global.General.UserName] = _userprofile.SurName;
                        }
                    }
                }
            }
            else
            {

                long _result = -1;
                _useraccount = new Account();
                _useraccount.Email = txtUserEmail.Text.Trim().ToLower();
                string _authcode = txtUserMobile.Text.Trim();
                _useraccount.AuthCode = _authcode;
                _useraccount.UserId = "CEUSERAUTOIDMOBILECI";
                _useraccount.UserTypeId = 400;
                _useraccount.IsAcceptTC = false;
                _useraccount.Status = true;
                _useraccount.RefCEUserID = -1;
                _result = _useraccountdb.Insert(_useraccount);
                _useraccount.CEID = _result;
                _retVal = _useraccount.CEID;
                if (_result > 0)
                {
                    _userprofile.Salutation = "";
                    _userprofile.FirstName = Server.HtmlEncode(txtUserName.Text);
                    _userprofile.SurName = "";
                    DateTime dteDOB = new DateTime(1995, 1, 1);
                    _userprofile.DOB = dteDOB;
                    _userprofile.Gender = 'M';

                    _userprofile.Occupation = 140;
                    _userprofile.Status = "Complete";
                    _userprofile.RefCEUserId = _useraccount.CEID;
                    _userprofile.IsAcceptTC = true;// chkAggrement.Checked;
                    _result = _userprofiledb.Insert(_userprofile);

                    _contact.Email = _useraccount.Email;
                    _contact.Mobile = txtUserMobile.Text.Trim();

                    _contact.Status = true;
                    _contact.RefCEUserId = _useraccount.CEID;
                    _result = _contactdb.Insert(_contact);

                    if (_result > 0)
                    {
                        Session[CE.Global.General.UserId] = _useraccount.CEID;
                        Session[CE.Global.General.UserTypeId] = _useraccount.UserTypeId;
                        Session[CE.Global.General.UserEmail] = _useraccount.Email;
                        Session[CE.Global.General.UserPhoneNo] = _contact.Mobile;
                        Session[CE.Global.General.StartTime] = General.GetUTCDateTime().ToString();
                        if (_userprofile.FirstName != string.Empty)
                        {
                            Session[CE.Global.General.UserName] = _userprofile.FirstName + " " + _userprofile.SurName;
                        }
                        else
                        {
                            Session[CE.Global.General.UserName] = _userprofile.SurName;
                        }
                        Session[CE.Global.General.UserProfileAndContactComplete] = "1";
                        Session[CE.Global.General.IsAcceptTC] = "0";
                        if (_userprofile.IsAcceptTC == true)
                        {
                            Session[CE.Global.General.IsAcceptTC] = "1";
                        }
                    }

                }

            }
        }
        else
        {
            _retVal = 0;
        }

        return _retVal;
        #endregion
    }

    private bool SendMailToUser(string EmailAddress, string UserName, string AuthCode)
    {
        bool _retVal = false;
        _retVal = CE.Email.Welcome.SendMail(EmailAddress, UserName, AuthCode);
        return _retVal;
    }

    private void UpdateUserIdCarDetails(Int64 CarId, Int64 UserId, Int64 ExistingUserId, string ExistingEmail, string ChangeStatus, string Email, string Name, string Mobile)
    {
        try
        {
            Int64 _result = -1;
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable();
            string _spName = "sprocCarDetailUpdateSingleItemByUserId";
            SortedList _sPara = new SortedList();
            _sPara.Add("@CarId", CarId);
            _sPara.Add("@RefCEUserId", UserId);
            _sPara.Add("@ExistingUserId", ExistingUserId);
            _sPara.Add("@ExistingEmail", ExistingEmail);
            _sPara.Add("@ChangeStatus", ChangeStatus);
            _sPara.Add("@Email", Email);
            _sPara.Add("@Name", Name);
            _sPara.Add("@Mobile", Mobile);

            _result = _db.ExecuteNonSqlSP(_spName, _sPara);
        }
        catch (Exception ex)
        {
        }
    }

    #endregion

}
